<?php

namespace Kylinlxl\QiniuOss;

use Encore\Admin\Form\Field;

class OssSimditor extends Field
{
    protected $view = 'qiniu-oss::oss-simditor';

    protected static $css = [
        'vendor/kylinlxl/qiniu-oss/simditor-2.3.25/styles/simditor.css',
    ];

    protected static $js = [
        'vendor/kylinlxl/qiniu-oss/simditor-2.3.25/scripts/module.js',
        'vendor/kylinlxl/qiniu-oss/simditor-2.3.25/scripts/hotkeys.js',
        'vendor/kylinlxl/qiniu-oss/simditor-2.3.25/scripts/uploader.js',
        'vendor/kylinlxl/qiniu-oss/simditor-2.3.25/scripts/simditor.js',
    ];

    public function render()
    {
        $token = csrf_token();

        $config = [
            'upload' => [
                'qnTokenUrl' => '/admin/qiniu/uptoken',
                'fileKey' => 'file',
                'connectionCount' => 3,
                'leaveConfirm' => '正在上传图片，你确定要离开吗?'
            ],
            'tabIndent' => true,
            'toolbar' => ['title', 'bold', 'italic', 'underline', 'strikethrough', 'fontScale', 'color', '|', 'ol', 'ul', 'blockquote', 'code', 'table', '|', 'link', 'image', 'hr', '|', 'indent', 'outdent', 'alignment'],
            'toolbarFloat' => true,
            'toolbarFloatOffset' => 0,
            'toolbarHidden' => false,
            'pasteImage' => true,
            'cleanPaste' => false,
        ];

        $userConfig = config('qiniu-oss.simditor.config');
        if(!empty($userConfig)) {
            $config = array_merge($config, $userConfig);
        }
        $config = json_encode((array)$config);

        $this->script = <<<EOT
        
var config = {$config}
config['textarea'] = $('#{$this->id}')
config['upload']['params'] = {_token: '{$token}'}
$(document).ready(function(){
      var simditorEditor = new Simditor(config);
 });
 
EOT;
        return parent::render();
    }
}
